题解 P3980 【[NOI2008]志愿者招募】

$Description$

有$n$天,每天需要$a_i$个志愿者,一共有$m$类志愿者可以招募。其中第$i$类可以从第$s_i$天工作到第$t_i$天,招募费用是每人$c_i$元。

$Solution$

边$(a,b)$表示边的容量为$a$费用为$b$

对于某一天为$i$

$i \stackrel{inf-a_{i}, 0}{\longrightarrow}i+1$

对于某一种志愿者$j$

$s_i \stackrel{inf,c_{i}}{\longrightarrow}t_i+1$

此外

$S \stackrel{inf,0}{\longrightarrow}1$

$n+1 \stackrel{inf,0}{\longrightarrow}T$

求最小费用最大流即可。

那么为什么要这么连边呢$?$

首先我们考虑,这样连最大流肯定$=inf$,由于求的是最小费用最大流,所以流会尽量往$i {\longrightarrow}i+1$这一类边走,但是由于这类边的边权为$inf-a_i$,所以必定至少有$a_i$的流往$s_i{\longrightarrow}t_i+1$这类边流,这就满足题意了。

$Code$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define pb push_back
#define N 40000
using namespace std;
struct edge{
int to,dis,w,next;
}e[1007000];
inline int read(){
int x=0,w=0;char ch=getchar();
while (!isdigit(ch))w|=ch=='-',ch=getchar();
while (isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return w?-x:x;
}
int cnt=1,head[N],dis[N],inque[N],vis[N],cur[N],cost,w[N],s,t;
inline void add(int u,int v,int d,int w){
e[++cnt].to=v;
e[cnt].dis=d;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt;
e[++cnt].to=u;
e[cnt].dis=0;
e[cnt].w=-w;
e[cnt].next=head[v];
head[v]=cnt;
}
bool spfa(){
memset(dis,0x3f,sizeof(dis));
queue<int>q;q.push(s);
dis[s]=0;
while (!q.empty()){
int u=q.front();q.pop();inque[u]=0;
for (int i=head[u];i;i=e[i].next){
int v=e[i].to;
if (e[i].dis&&dis[v]>dis[u]+e[i].w){
dis[v]=dis[u]+e[i].w;
if (!inque[v])
q.push(v),inque[v]=1;
}
}
}
return dis[t]<inf;
}
int dfs(int u,int mn){
vis[u]=1;
if (u==t)return mn;
int used=0,mi;
for (int &i=cur[u];i;i=e[i].next){
int v=e[i].to;
if ((!vis[v]||v==t)&&e[i].dis&&dis[v]==dis[u]+e[i].w)
if (mi=dfs(v,min(e[i].dis,mn-used))){
e[i].dis-=mi;
e[i^1].dis+=mi;
used+=mi;
cost+=e[i].w*mi;
if (mn==used)break;
}
}
return used;
}
int Dinic(){
int res=0;
while (spfa()){
vis[t]=1;
while (vis[t]){
memset(vis,0,sizeof(vis));
for (int i=s;i<=t;++i)cur[i]=head[i];
res+=dfs(s,inf);
}
}
return res;
}
signed main(){
int n=read(),m=read();s=0,t=n+2;
for (int i=1;i<=n;++i)
w[i]=read();
add(s,1,inf,0),add(n+1,t,inf,0);
for (int i=1;i<=n;++i)add(i,i+1,inf-w[i],0);
for (int i=1;i<=m;++i){
int a=read(),b=read(),d=read();
add(a,b+1,inf,d);
}
Dinic();
printf("%d\n",cost);
return 0;
}